package com.li.blog.controller;

import com.li.blog.common.cache.Cache;
import com.li.blog.service.ArticleService;
import com.li.blog.vo.ArticleVo;
import com.li.blog.vo.Result;
import com.li.blog.vo.params.ArticleParam;
import com.li.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//json数据交互
@RestController
@RequestMapping("articles")
public class ArticleController {

  @Autowired
  private ArticleService articleService;
  /**
   * 首页 文章列表
   * @param pageParams
   */
  @PostMapping
  @Cache(expire = 5 * 60 * 1000,name = "listArticle")
  public Result listArticle(@RequestBody PageParams pageParams){
     return articleService.listArticle(pageParams);
  }

  @PostMapping("hot")
  @Cache(expire = 5 * 60 * 1000,name = "hot_article")
  public Result hotArticle(){
    int limit = 5;
    return articleService.hotArticle(limit);
  }

  @PostMapping("new")
  public Result newArticles(){
    int limit = 5;
    return articleService.newArticles(limit);
  }

  @PostMapping("listArchives")
  public Result listArchives(){
    return articleService.listArchives();
  }

  @PostMapping("view/{id}")
  public Result findArticlesById(@PathVariable("id") long id){
    ArticleVo articleVo = articleService.findArticlerById(id);
    return Result.success(articleVo);
  }

  @PostMapping("publish")
  public Result publish(@RequestBody ArticleParam articleParam){
    return articleService.publish(articleParam);
  }
}
